package com.gizwits.opensource.appkit.yasystem;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.gizwits.gizwifisdk.api.GizWifiDevice;
import com.gizwits.gizwifisdk.enumration.GizWifiErrorCode;
import com.gizwits.gizwifisdk.listener.GizWifiDeviceListener;
import com.gizwits.opensource.appkit.ControlModule.GosControlModuleBaseActivity;
import com.gizwits.opensource.appkit.R;

import java.util.concurrent.ConcurrentHashMap;

public class Main3Activity extends GosControlModuleBaseActivity {
    Button button11,button12,btn1;
    private LocalBroadcastManager localBroadcastManager;
    MyThread m;
    String data;
    TextView mian;
    int wan = 0,shui =0;
    ImageView imageView;
    EditText edtemp,edtime,edspeed;
    RadioButton rad1,rad2,rad3,rad4;
    boolean run =true;
    TextView tv1,tv2,tv3,shui1;
    private GizWifiDevice device;
    GosControlModuleBaseActivity go;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        localBroadcastManager = LocalBroadcastManager.getInstance(this);
        go = new GosControlModuleBaseActivity();
        Intent intent = getIntent();
        if (intent!= null) {
            device = (GizWifiDevice) intent.getParcelableExtra("GizWifiDevice");
            device.setListener(mGizWifiDeviceListener);
        }
        else{
            Toast.makeText(Main3Activity.this,"网络故障",Toast.LENGTH_SHORT).show();
        }
        initView();
        initEvent();
    }

    private void initView(){
        btn1 = (Button) findViewById(R.id.btn1);
        rad1 = (RadioButton) findViewById(R.id.RadioButton01);
        rad2 = (RadioButton) findViewById(R.id.RadioButton02);
        rad3 = (RadioButton) findViewById(R.id.RadioButton03);
        rad4 = (RadioButton) findViewById(R.id.RadioButton04);

        edtemp = (EditText) findViewById(R.id.edtemp);
        edtime = (EditText) findViewById(R.id.edtime);
        edspeed = (EditText) findViewById(R.id.edspeed);
        mian = (TextView) findViewById(R.id.mian);
        shui1 = (TextView) findViewById(R.id.shui);
        tv1 = (TextView) findViewById(R.id.ed1);
        tv2 = (TextView) findViewById(R.id.ed2);
        tv3 = (TextView) findViewById(R.id.ed3);
        imageView= (ImageView) findViewById(R.id.image);
        button11 = (Button) findViewById(R.id.btn12);
        button12 = (Button) findViewById(R.id.btn13);
    }

    private void initEvent(){
        imageView.setOnClickListener(onClickListener);
        button11.setOnClickListener(onClickListener);
        button12.setOnClickListener(onClickListener);
    }
    Button.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.btn1:
                    run =false;
                    sure();
                    break;
                case R.id.btn12:
                    sure();
                    break;
                case R.id.btn13:
                    data = "ScloseautoE";
                    retun(data);
                    finish();
                    break;
                case R.id.image:
                    data = "ScloseautoE";
                    retun(data);
                    finish();
                    break;
                default:
                    break;
            }
        }
    };

    public void sendCommand(String key, Object value) {
        if (value == null) {
            return;
        }
        int sn = 5;
        ConcurrentHashMap<String, Object> hashMap = new ConcurrentHashMap<String, Object>();
        hashMap.put(key, value);
        // 同时下发多个数据点需要一次性在map中放置全部需要控制的key，value值
        // hashMap.put(key2, value2);
        // hashMap.put(key3, value3);
        device.write(hashMap, sn);
        Log.i("liang", "下发命令：" + hashMap.toString());
    }

    private void sure(){

        new Thread(new MyThread()).start();
    }

    public class  MyThread implements Runnable{
        @Override

        public void run() {
            run =true;
            while(run){
                boolean a =String.valueOf(edtemp.getText()).equals(String.valueOf(tv1.getText()));
                boolean b = String.valueOf(edtime.getText()).equals(String.valueOf(tv2.getText()));
                boolean c = String.valueOf(edspeed.getText()).equals(String.valueOf(tv3.getText()));
                if (a&&b&&c){
                    run=false;
                    handler.sendEmptyMessage(101);
                }
                else{
                    try {
                        boolean a1 = String.valueOf(mian.getText()).equals("小碗面");
                        boolean a2 = String.valueOf(mian.getText()).equals("大碗面");
                        boolean b1 = String.valueOf(shui1.getText()).equals("小碗水");
                        boolean b2 = String.valueOf(shui1.getText()).equals("大碗水");
                        if (rad1.isChecked()&&!a1){
                            sendCommand("MODMIAN",1);
                        }
                        if(rad2.isChecked()&&!a2){
                            sendCommand("MODMIAN",2);
                        }
                        if (rad3.isChecked()&&!b1){
                            sendCommand("MODSHUI",1);
                        }
                        if (rad4.isChecked()&&!b2){
                            sendCommand("MODSHUI",2);
                        }
                        if(!a){
                            sendCommand("MODTEMP",String.valueOf(edtemp.getText()));
                        }
                        if (!b)
                        sendCommand("MODTIME",String.valueOf(edtime.getText()));
                        if (!c)
                        sendCommand("MODSPEED",String.valueOf(edspeed.getText()));
                        sendCommand("SmensureE",true);
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    handler.sendEmptyMessage(100);
                }
            }
        }
    };
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.what ==101){
                Toast.makeText(Main3Activity.this,"发送成功",Toast.LENGTH_SHORT).show();
            }
            else if (msg.what ==100){
                Toast.makeText(Main3Activity.this,"正在发送",Toast.LENGTH_SHORT).show();
            }
        }
    };

    private GizWifiDeviceListener mGizWifiDeviceListener= new GizWifiDeviceListener(){
        //接收数据回调
        @Override
        public void didReceiveData(GizWifiErrorCode result, GizWifiDevice device, ConcurrentHashMap<String, Object> dataMap, int sn) {
            super.didReceiveData(result, device, dataMap, sn);
            if (result == GizWifiErrorCode.GIZ_SDK_SUCCESS && dataMap.get("data") != null) {
                Log.d("接收到的数据", dataMap.toString());
                go.getDataFromReceiveDataMap(dataMap);
                updateUI();
            }
        }
    };

    @Override
    protected void onDestroy() {
        run = false;
        data = "ScloseautoE";
        retun(data);;
        super.onDestroy();
    }

    protected void updateUI() {
        if (data_MODSHUI==1){
            shui1.setText("小碗水");
        }else
            shui1.setText("大碗水");

        if (data_MODMIAN==1){
            mian.setText("小碗面");
        }else{
            mian.setText("大碗面");
        }
        tv3.setText(String.valueOf(data_MODSPEED));
        tv2.setText(String.valueOf(data_MODTIME));
        tv1.setText(String.valueOf(data_MODTEMP));
    }
    public void retun(String data){
        Intent intent = new Intent("com.example.broadcasttest.LOCAL_BROADCAST");
        intent.putExtra("key",data);
        intent.putExtra("data",true);
        localBroadcastManager.sendBroadcast(intent);
    }
}
